package com.huiquan.framework.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * ClassName:DateUtils Description:
 *
 * @author ChaoKai Wen email:chaokai.wen@witspring.com
 * @Date 14/11/25
 * @since Ver 1.0
 */
public class DateUtils {

	public static enum DateFormat {

		SHORT("yy-MM-dd"), DATE("yyyy-MM-dd"), SIMPLE("yyyyMMdd"), FULL("yyyy-MM-dd HH:mm:ss"), YMDHM(
				"yyyyMMddHHmm"), MINITUE(
						"yyyyMMdd HHmm"), FULL_SECOND("yyyyMMddHHmmss"), YEAR_MONTH("yyyyMM"), TIME("HH:mm:ss");

		private String format;
		private ThreadLocal<SimpleDateFormat> convert;

		DateFormat(final String format) {
			this.format = format;
			convert = new ThreadLocal<SimpleDateFormat>() {
				@Override
				protected SimpleDateFormat initialValue() {
					return new SimpleDateFormat(format);
				}
			};
		}

		public String getFormat() {
			return format;
		}

		public ThreadLocal<SimpleDateFormat> getConvert() {
			return convert;
		}
	}

	public static Integer retrievePreDate(String date) {
		Calendar cal = new GregorianCalendar();

		SimpleDateFormat oSdf = new SimpleDateFormat("", Locale.ENGLISH);
		oSdf.applyPattern("yyyyMM");
		try {
			cal.setTime(oSdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		cal.add(Calendar.MONTH, -1);

		return Integer.parseInt(DateFormat.YEAR_MONTH.getConvert().get().format(cal.getTime()));
	}

	public static Integer cauculateActualDay(String date) {
		Calendar cal = new GregorianCalendar();

		SimpleDateFormat oSdf = new SimpleDateFormat("", Locale.ENGLISH);
		oSdf.applyPattern("yyyyMM");
		try {
			cal.setTime(oSdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static Long distinctDays(String fromDay, String toDay) {
		SimpleDateFormat df = DateFormat.DATE.getConvert().get();
		try {
			long to = df.parse(toDay).getTime();
			long from = df.parse(fromDay).getTime();
			return ((to - from) / (1000 * 60 * 60 * 24));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return -1L;
	}

	public static Boolean verifyDateFormat(String date, DateFormat format) {
		try {
			format.getConvert().get().parse(date);
			return Boolean.TRUE;
		} catch (ParseException e) {
			return Boolean.FALSE;
		}
	}

	public static String convertDateFormat(Date date, DateFormat format) {
		return format.getConvert().get().format(date);
	}

	public static String convertTodayFormat() {
		return convertDateFormat(new Date(), DateFormat.DATE);
	}

	public static String convertCurrentTimeFormat() {
		return convertDateFormat(new Date(), DateFormat.FULL);
	}

	public static Date convertDateText(String date, DateFormat format) {
		try {
			return format.getConvert().get().parse(date);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Long retrieveTodayStartTime() {

		// 获取当前服务器12点信息
		Calendar currentDate = new GregorianCalendar();

		currentDate.set(Calendar.HOUR_OF_DAY, 0);
		currentDate.set(Calendar.MINUTE, 0);
		currentDate.set(Calendar.SECOND, 0);

		return currentDate.getTime().getTime() / 1000 * 1000;

	}

	public static Date getNextDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		Date date1 = new Date(calendar.getTimeInMillis());
		return date1;
	}

	public static Date getPreNDate(Date date, Integer diff) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, diff * -1);
		Date date1 = new Date(calendar.getTimeInMillis());
		return date1;
	}

	/**
	 * 转换string格式的日期为long型
	 *
	 * @param date
	 * @return
	 */
	public static Long convertSimpleDateToLong(String date) {
		try {
			return DateFormat.SIMPLE.getConvert().get().parse(date).getTime();
		} catch (Exception e) {
			// LOG.warn("yyyy-MM-dd HH:mm format failed, user default value");
			return -1L;
		}
	}

	/**
	 * 转换string格式的日期为long型
	 *
	 * @param date
	 * @return
	 */
	public static Long convertDateToLong(String date) {
		try {
			return DateFormat.DATE.getConvert().get().parse(date).getTime();
		} catch (Exception e) {
			return -1L;
		}
	}

	public static Long convertDateToLong(String date, DateFormat format) {
		try {
			return format.getConvert().get().parse(date).getTime();
		} catch (Exception e) {
			return -1L;
		}
	}

	public static String formatTimeMillis(Long timeMillis) {
		timeMillis = timeMillis / 1000;
		StringBuilder sb = new StringBuilder();
		if (timeMillis / 3600 > 0) {
			sb.append(timeMillis / 3600);
			sb.append("h");
			timeMillis = timeMillis % 3600;
		}
		if (timeMillis / 60 > 0) {
			sb.append(timeMillis / 60);
			sb.append("m");
			timeMillis = timeMillis % 60;
		}
		sb.append(timeMillis);
		sb.append("s");
		return sb.toString();
	}

	public static Calendar getCalendarToday() {
		Calendar today = Calendar.getInstance();
		today.setTime(new Date());
		return today;
	}

	public static Calendar getCalendarBeforeToday(int i) {
		Calendar day = getCalendarToday();
		day.add(Calendar.DAY_OF_MONTH, -1 * i);
		return day;
	}

	// public static Calendar getInternetBJTime() {
	// Calendar now = null;
	// try {
	// URL url=new URL("http://www.baidu.com");//取得资源对象
	// URLConnection uc=url.openConnection();//生成连接对象
	// uc.connect(); //发出连接
	//
	// long ld=uc.getDate(); //取得网站日期时间
	// Date date=new Date(ld); //转换为标准时间对象
	// now = Calendar.getInstance();
	// now.setTime(date);
	// } catch (MalformedURLException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return now;
	// }

	public static String getTimeStr() {
		// Calendar now = getInternetBJTime();
		// if(now == null) {
		// now = Calendar.getInstance();
		// }
		Calendar now = Calendar.getInstance();

		SimpleDateFormat df = DateFormat.TIME.getConvert().get();
		return df.format(now.getTime());
	}

	/**
	 * 根据UNIX时间戳得到日期字符串
	 * 
	 * @param milliseconds
	 * @return
	 */
	public static String getDateStrFromUnixTimestamp(String unixTimestamp) {
		Date date = new Date(Long.valueOf(unixTimestamp) * 1000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		return dateStr;
	}

	public static String formatCalendarDate(Calendar c) {
		
		SimpleDateFormat df = DateFormat.DATE.getConvert().get();
		return df.format(c.getTime());
	}
}
